package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.penpal.adapter.PenpalNoticeAdapter;
import com.rongshu.view.penpal.bean.PenpalNoticeResponse;
import com.rongshu.view.penpal.present.PenpalNoticePresenter;
import com.rongshu.view.penpal.view.NoticeAdapterListener;
import com.rongshu.view.penpal.view.PenpalNoticeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/26.
 * description：
 */
public class PenpalNoticeActivity extends BaseActivity<PenpalNoticePresenter> implements PenpalNoticeView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_right_iv)
    ImageView headRight;
    @BindView(R.id.notice_list)
    RecyclerView noticeList;
    @BindView(R.id.view_stub_empty)
    ViewStub empty;
    private View inflate;
    private RelativeLayout layoutEmpty;
    private int penpalwill_id;
    private PenpalNoticeAdapter adapter;
    private List<PenpalNoticeResponse.ResponseBean> list;
    private int ADD_PENPAL_NOTICE = 101;

    @Override
    protected int initLayout() {
        return R.layout.activity_penpal_notice;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            penpalwill_id = intent.getIntExtra(Constant.PENPAL_ID, 0);

        }
        headMiddle.setText("公告管理");
        list = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        noticeList.setLayoutManager(manager);
        adapter = new PenpalNoticeAdapter(this, list);
        noticeList.setAdapter(adapter);
        adapter.setAdapterListener(new NoticeAdapterListener() {
            @Override
            public void onItemListener(PenpalNoticeResponse.ResponseBean bean) {
                Intent intent = new Intent(PenpalNoticeActivity.this, PenpalEditNoticeActivity.class);
                intent.putExtra(Constant.PENPAL_NOTICE, bean);
                startActivityForResult(intent, ADD_PENPAL_NOTICE);
            }

            @Override
            public void onDeleteListener(PenpalNoticeResponse.ResponseBean bean) {
                presenter.deleteNotice(penpalwill_id, bean.getId());
            }

            @Override
            public void onSetTopListener(PenpalNoticeResponse.ResponseBean bean) {
                presenter.setTopNotice(penpalwill_id, bean.getId());
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalNoticePresenter(this), this);
        presenter.getNotice(penpalwill_id);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_left, R.id.head_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;
            case R.id.head_right:
                Intent intent = new Intent(this, PenpalEditNoticeActivity.class);
                intent.putExtra(Constant.PENPAL_ID, penpalwill_id);
                startActivityForResult(intent, ADD_PENPAL_NOTICE);
                break;
        }
    }

    @Override
    public void callBackNotces(PenpalNoticeResponse response) {
        if (response != null && response.getResponse() != null) {
            list.clear();
            list.addAll(response.getResponse());
            adapter.notifyDataSetChanged();
            if (list.size() == 0){
                showEmptyLayout();
            } else {
                hideEmptyLayout();
            }
        }
    }

    @Override
    public void callBackAddNoticeResult() {
        presenter.getNotice(penpalwill_id);
    }

    @Override
    public void callBackDeleteNoticeResult() {
        presenter.getNotice(penpalwill_id);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ADD_PENPAL_NOTICE) {
                presenter.getNotice(penpalwill_id);
            }
        }
    }

    private void showEmptyLayout() {
        if (inflate == null) {
            inflate = empty.inflate();
            layoutEmpty = inflate.findViewById(R.id.layout_empty);
            ImageView noDataImg = inflate.findViewById(R.id.no_data_img);
            TextView noDataHint = inflate.findViewById(R.id.no_data_hint);
            noDataImg.setImageResource(R.drawable.empty_notice);
            noDataHint.setText("暂无公告");
        }
        layoutEmpty.setVisibility(View.VISIBLE);
    }

    private void hideEmptyLayout() {
        if (layoutEmpty != null) {
            layoutEmpty.setVisibility(View.GONE);
        }
    }
}
